package com.sxt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.sxt.copy.Jsoup1;
import com.sxt.copy.Jsoup2;
import com.sxt.copy.Jsoup3;
import com.sxt.entity.Article;
import com.sxt.service.ArticleService;



public class CSDN {
	 // 获取img标签正则
    private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
	private static final String BiUrl = "https://www.aicoin.cn/news/all";
	private static String biUrl = "http://www.bishijie.com/shendu_";
	public static List<String> getUrl() throws IOException{
		Document doc = Jsoup.connect(BiUrl).get();
		Elements els = doc.select("div.container");
		List<String> list = new ArrayList<String>();
		for (Element element : els) {
			String pageView = element.select("span.pageView").text();
			int count = Integer.parseInt(pageView);
			if(count>1000){
			String href = element.select("div.container").attr("id");
			  list.add(biUrl+href);
			}
		}
		return list;
	}
	 public static List<String> getImageUrl(String html) {
    	 Matcher matcher=Pattern.compile(IMGURL_REG).matcher(html);
         List<String>listimgurl=new ArrayList<String>();
         while (matcher.find()){
             listimgurl.add(matcher.group());
         }
         return listimgurl;
	}
	  //获取ImageSrc地址
     public static List<String> getImageSrc(List<String> listimageurl){
         List<String> listImageSrc=new ArrayList<String>();
         for (String url : listimageurl) {
        	 Document doc = Jsoup.parse(url);
        	 Elements link = doc.select("img");
        	 String src = link.attr("src");
        	 listImageSrc.add(src);
		}
        
         return listImageSrc;
     }
     //下载图片
     public static String Download(List<String> listImgSrc,String html) {
    	 String Article = null;
    	 List<String> list = new ArrayList<String>();
    	 try {
             //开始时间
             Date begindate = new Date();
             int i = 1;
             for (String url : listImgSrc) {
                 //开始时间
            	 
                 Date begindate2 = new Date();
                 String imageName =UUID.randomUUID()+"" ;
                 URL uri = new URL(url);
                 InputStream in = uri.openStream();
                 FileOutputStream fo = new FileOutputStream(new File("D:/imgs/"+imageName+".jpg"));//文件输出流
                 byte[] buf = new byte[1024];
                 int length = 0;
                 System.out.println("开始下载:" + url);
                 while ((length = in.read(buf, 0, buf.length)) != -1) {
                     fo.write(buf, 0, length);
                 }
                 //关闭流
                 in.close();
                 fo.close();
                 System.out.println(imageName + "下载完成");
                 //结束时间
                 Date overdate2 = new Date();
                 double time = overdate2.getTime() - begindate2.getTime();
                 System.out.println("耗时：" + time / 1000 + "s");
             }
             Date overdate = new Date();
             double time = overdate.getTime() - begindate.getTime();
             System.out.println("总耗时：" + time / 1000 + "s");
           //开始替换
             List<String> slist = new ArrayList<String>();
             File dir = new File("D:/imgs/");
             File [] fs = dir.listFiles();
             for (File file : fs) {
            	 if(file.exists() && !file.isDirectory() && file.getName().endsWith(".jpg")){
            		 String src = file.getAbsolutePath();
            		 slist.add(src);
            	   }else {
                  	 throw new Exception("图片未找到!!!!");
                   }
            	 }
             
             Document doc = Jsoup.parse(html);
        	    Elements link = doc.select("img");
        	    int s=0;
        	    for (Element element : link) {
        	    	element.attr("src", "File://"+slist.get(s++));
				}
        	    Article = doc.toString();
         } catch (Exception e) {
             System.out.println("下载失败");
         }
		return Article;
     }
    public static void add(String url){
    	 List<String> list = Jsoup3.addArticle(url, 
					"div.article h1", 
					"div.txt");
    	 String content1 = list.get(1);
    	 List<String> listimageurl = CSDN.getImageUrl(content1);
    	 List<String> listsrc = CSDN.getImageSrc(listimageurl);
    	 String content = CSDN.Download(listsrc, content1);
    	 
 	//启动Spring容器
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		ArticleService articleService=(ArticleService) context.getBean("articleService");
		Article art =new Article();
		
		String id =String.valueOf((int) (Math.random()*1000000000));
       art.setId(id);
       art.setTitle(list.get(0));
       art.setTags("Dapp");
       art.setAuthorId("1535422958143");
       art.setContent(content);
       art.setCommentCount(0);
   	  art.setViewCount(0);
       art.setRewardContent("");
   	  art.setRewardPoint(0);
       art.setPermalink("/article/"+art.getId());
       Long l = System.currentTimeMillis();
        String date = l.toString();
   	  art.setCreateTime(new BigInteger(date));
       art.setUpdateTime(new BigInteger(date));
   	  art.setLatestCmtTime(new BigInteger("0"));
       art.setLatestCmterName("");
       art.setRandomDouble(Math.random());
       art.setCommentable("1");
       
       art.setEditorType(0);
       art.setArticleStatus(0);
   	  art.setArticleType(0);
       art.setGoodCnt(0);
   	  art.setBadCnt(0);
       art.setCollectCnt(0);
       art.setWatchCnt(0);
       art.setRedditScore(0d);
       art.setCity("");
       art.setIP("127.0.0.1");
       art.setUA("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
       art.setStick(new BigInteger("0"));
       art.setAnonymous(0);
       art.setPerfect(0);
       art.setAnonymousView(0);
       art.setAudioURL("");
       art.setQnAOfferPoint(0);
       art.setPushOrder(0);
		articleService.register(art);
    	
    	
    }
    
	public static void main(String[] args) throws Exception{
		List<String> urls = new ArrayList<String>();
		 
		 urls.add("http://sports.mop.com/a/180711232752016000000.html");
		

		 
		
		 
		 
		 for (String url : urls) {
			 add(url);
		}
     
     }
}
